package dao;


import pojo.User;
import util.DBUtil;
import util.DBUtilImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author: zen
 * @date: 2020/4/12
 * @Title: UserDaoImpl
 * @Description:
 */
public class UserDaoImpl implements UserDao {

    private Connection conn = null;
    private PreparedStatement pstmt = null;
    private ResultSet rs = null;

    /**
     * 添加用户
     * @param user
     * @return
     */
    @Override
    public boolean addUser(User user) {
        boolean flag = true;
        DBUtil dbUtil = DBUtilImpl.getSingleton();
        conn = dbUtil.getConnection();
        // 没有获取连接
        if (conn == null) {
            flag = false;
        }
        String sql = "insert into user(account, pass, name, gender, age, address, role) values (?,?,?,?,?,?,?)";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getAccount());
            pstmt.setString(2, user.getPass());
            pstmt.setString(3, user.getName());
            pstmt.setString(4, user.getGender());
            pstmt.setInt(5, user.getAge());
            pstmt.setString(6, user.getAddress());
            pstmt.setInt(7, user.getRole());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            flag = false;

        } finally {
            dbUtil.close(conn, pstmt, rs);

        }
        return flag;
    }

    /**
     * 判断账户是否重复
     * @param account
     * @return
     */
    @Override
    public boolean isExistence(String account) {
        boolean flag = true;
        DBUtil dbUtil = DBUtilImpl.getSingleton();
        conn = dbUtil.getConnection();
        // 没有获取连接
        if (conn == null) {
            return false;
        }
        String sql = "select * from user where account =?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, account);
            rs = pstmt.executeQuery();
            if (!rs.next()) {
                flag = false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            flag = false;
        } finally {
            dbUtil.close(conn, pstmt, rs);
        }
        return flag;
    }

    /**
     * 用户登录验证
     * @param account
     * @param pass
     * @return
     */
    @Override
    public boolean isExistence(String account, String pass) {
        boolean flag = true;
        DBUtil dbUtil = DBUtilImpl.getSingleton();
        conn = dbUtil.getConnection();
        if (conn == null) {
            return false;
        }
        String sql = "select * from user where account = ? and pass = ?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, account);
            pstmt.setString(2, pass);
            rs = pstmt.executeQuery();
            if (!rs.next()) {
                flag = false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            flag = false;
        } finally {
            dbUtil.close(conn, pstmt, rs);
        }
        return flag;
    }


    /**
     * 根据账户获取用户信息
     * @param account
     * @return
     */
    @Override
    public User getUserByAccount(String account) {
        User user = new User();
        DBUtil dbUtil = DBUtilImpl.getSingleton();
        conn = dbUtil.getConnection();
        String sql = "select * from user where account = ?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, account);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                user.setUserId(rs.getInt("user_id"));
                user.setAccount(rs.getString("account"));
                user.setPass(rs.getString("pass"));
                user.setName(rs.getString("name"));
                user.setGender(rs.getString("gender"));
                user.setAge(rs.getInt("age"));
                user.setAddress(rs.getString("address"));
                user.setRole(rs.getInt("role"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            dbUtil.close(conn, pstmt, rs);
        }
        return user;
    }


    /**
     * 修改用户密码
     * @param user
     * @return
     */
    @Override
    public boolean modifyPassByUser(User user) {
        boolean flag = false;
        DBUtil dbUtil = DBUtilImpl.getSingleton();
        conn = dbUtil.getConnection();
        if (conn == null) {
            return flag;
        }
        String sql = "update user set pass=? where account=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getPass());
            pstmt.setString(2, user.getAccount());
            pstmt.executeUpdate();
            // 更新成功，返回结果
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            dbUtil.close(conn, pstmt, rs);
        }
        return flag;
    }


    /**
     * 修改用户资料
     * @param user
     * @return
     */
    @Override
    public boolean modifyInfoByUser(User user) {
        boolean flag = false;
        DBUtil dbUtil = DBUtilImpl.getSingleton();
        conn = dbUtil.getConnection();
        if (conn == null) {
            return flag;
        }
        String sql = "update user set name=?,gender=?,age=?,address=? where user_id=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getName());
            pstmt.setString(2, user.getGender());
            pstmt.setInt(3, user.getAge());
            pstmt.setString(4, user.getAddress());
            pstmt.setInt(5, user.getUserId());
            pstmt.executeUpdate();
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            dbUtil.close(conn, pstmt, rs);
        }
        return flag;
    }


    /**
     * 检查用户名是否可用
     * @param name
     * @return
     */
    @Override
    public boolean isAvailableName(String name) {
        boolean flag = false;
        DBUtil dbUtil = DBUtilImpl.getSingleton();
        conn = dbUtil.getConnection();
        if (conn == null) {
            return flag;
        }
        String sql = "select * from user where name = ?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            rs = pstmt.executeQuery();
            if (!rs.next()) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            dbUtil.close(conn, pstmt, rs);
        }
        return flag;
    }
}
